package com.mohan.calligraphy.util;

import com.mohan.calligraphy.dto.DtoBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.Nullable;

import java.util.function.BiConsumer;

@Slf4j
public final class BeanUtil {

    public static <S extends Object,T extends DtoBase> T toDto(S obj, Class<T> targetClass){
        return toDto(obj,targetClass,null);
    }

    public static <S extends Object,T extends DtoBase> T toDto(S obj, Class<T> targetClass, @Nullable BiConsumer<S,T> consumer) {
        if (obj == null) {
            return null;
        }
        T dto = BeanUtils.instantiateClass(targetClass);
        if (consumer != null) {
            consumer.accept(obj,dto);
        }
        return dto;
    }
}
